﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LimitlessLightSwitch.Helper
{
  /// <summary>
  /// The application settings
  /// </summary>
  public class AppSettings
  {
    #region Logic
    IsolatedStorageSettings settings;
    const string HostNameSettingKeyName = "HostNameSetting";
    const string PortSettingKeyName = "PortSetting";
    const String HostNameSettingDefault = "192.168.1.1";
    const String PortSettingDefault = "8899";
    /// <summary>
    /// Constructor that gets the application settings.
    /// </summary>
    public AppSettings()
    {
      // Get the settings for this application.
      settings = IsolatedStorageSettings.ApplicationSettings;
    }

    /// <summary>
    /// Update a setting value for our application. If the setting does not
    /// exist, then add the setting.
    /// </summary>
    /// <param name="Key"></param>
    /// <param name="value"></param>
    /// <returns></returns>
    public bool AddOrUpdateValue(string Key, Object value)
    {
      bool valueChanged = false;

      // If the key exists
      if (settings.Contains(Key))
      {
        // If the value has changed
        if (settings[Key] != value)
        {
          // Store the new value
          settings[Key] = value;
          valueChanged = true;
        }
      }
      // Otherwise create the key.
      else
      {
        settings.Add(Key, value);
        valueChanged = true;
      }
      return valueChanged;
    }

    /// <summary>
    /// Get the current value of the setting, or if it is not found, set the 
    /// setting to the default setting.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="Key"></param>
    /// <param name="defaultValue"></param>
    /// <returns></returns>
    public T GetValueOrDefault<T>(string Key, T defaultValue)
    {
      T value;

      // If the key exists, retrieve the value.
      if (settings.Contains(Key))
      {
        value = (T)settings[Key];
      }
      // Otherwise, use the default value.
      else
      {
        value = defaultValue;
      }
      return value;
    }

    /// <summary>
    /// Save the settings.
    /// </summary>
    public void Save()
    {
      settings.Save();
    } 
    #endregion

    #region Settings
    /// <summary>
    /// Gets or sets the host name setting.
    /// </summary>
    /// <value>
    /// The host name setting.
    /// </value>
    public String HostNameSetting
    {
      get
      {
        return GetValueOrDefault<String>(HostNameSettingKeyName, HostNameSettingDefault);
      }
      set
      {
        if (AddOrUpdateValue(HostNameSettingKeyName, value))
        {
          Save();
        }
      }
    }

    /// <summary>
    /// Gets or sets the port setting.
    /// </summary>
    /// <value>
    /// The port setting.
    /// </value>
    public String PortSetting
    {
      get
      {
        return GetValueOrDefault<String>(PortSettingKeyName, PortSettingDefault);
      }
      set
      {
        if (AddOrUpdateValue(PortSettingKeyName, value))
        {
          Save();
        }
      }
    } 
    #endregion

  }
}